package com.leyou.web;

import com.leyou.dto.SpecGroupDTO;
import com.leyou.dto.SpecParamDTO;
import com.leyou.service.SpecGroupService;
import com.leyou.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;//规格
    @Autowired
    private SpecParamService  specParamService;//规格参数

    public SpecController (SpecGroupService specGroupService,SpecParamService specParamService){
        this.specGroupService=specGroupService;
        this.specParamService=specParamService;
    }
    /*
    * TODO 根据商品分类id，查询规格组的集合
    *  */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listQueryGroupByCategory(
            @RequestParam("id") Long cid){
        return ResponseEntity.ok(this.specGroupService
                .listQueryGroupByCategory(cid));
    }

    /*
    *todo
    *   根据查询条件动态查询规格参数集合
    * */
    @GetMapping("/params")
    public  ResponseEntity<List<SpecParamDTO>> listSpecParam(
            @RequestParam(value = "categoryId" ,required =false) Long cid,
            @RequestParam(value = "searching",required = false) Boolean searching,
            @RequestParam(value = "groupId",required = false) Long gid){
        return ResponseEntity.ok(this.specParamService.listSpecParam(cid,searching,gid));
    }

    /*
    todo
       新增规格组
      */
    @PostMapping("/group")
    public ResponseEntity<Void>saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.specGroupService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /*
    * todo
    *  修改规格组
    * */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody  SpecGroupDTO specGroupDTO){
        this.specGroupService.updateGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /*
    * todo
    *  新增规格参数
    * */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.saveSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /*
    * todo
    *   修改规格参数
    * */
    @PutMapping("/param")
    public ResponseEntity<Void>updateParam(@RequestBody SpecParamDTO specParamDTO){
        this.specParamService.updateParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


/*
  todo
   查询规格组及规格参数
         根据分类id查询规格参数组集合以及每个规格参数组对应的组内规格参数
 */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>>listSpecGroupWithParams(@RequestParam("id") Long cid){
        return ResponseEntity.ok(this.specGroupService.listSpecGroupWithParams(cid));
    }



}
